/**
 * This program is free software but WITHOUT ANY WARRANTY OR CONDITIONS OF ANY KIND.
 *
 * Copyright (C) 2008-2012 www.pretty-tools.com
 */

// min delay for slide show
var MIN_SLIDESHOW_DELAY = 1;

/**
 * Slide Show.
 *
 * @author Alexander Kozlov (alex@pretty-tools.com)
 */
var gSlideShow = {
    logger: yrprxFactory.createLogger("gSlideShow"),
    // array with source urls
    imgSources: [],
    // index of image that is loading
    idxInProgress: -1,
    // ref to image in progress
    imgInProgress: null,
    //loading in-progress
    loadingInProgress: false,
    // count of loaded images
    loadedCount: 0,
    // index of current visible image
    idxCurrent: -1,
    // time elapsed
    timeElapsed: 0,
    // slide show speed
    interval: 3,
    // play mode
    playMode: false,
    // shrink
    shrinkMode: false,
    // zoom
    zoomMode: false,
    // repeat
    repeatMode: false,
    // timer instance
    timer: null,

    // additional data to simplify image manipulations

    // visible image reference
    imgCurrent: null,
    // current image info
    imgCurrentInfo: {
        position: {
            left:0,
            top:0
        },
        size: {
            width:0,
            height:0
        },
        originalSize: {
            width:0,
            height:0
        }
    },
    // min
    minSize: {
        width: wsshConfig.minImageWidth,
        height: wsshConfig.minImageHeight
    },
    // step to scroll image
    scrollStep: 50,
    zoomPercent: 20
};

gSlideShow.onLoad = function (event)
{
    try
    {
        this.imgSources = window.arguments[0];

        if (this.logger.enabled) this.logger.debug("onLoad: count=" + this.imgSources.length);

        var sourcePageTitle = window.arguments.length >= 2 ? window.arguments[1] : "";
        if (sourcePageTitle != "") sourcePageTitle = " - " + sourcePageTitle;

        var fullScreenMode = this.imgSources.length == 1;
        if (fullScreenMode)
        {
            var panel_screen = document.getElementById("slideshow_panel_screen");
            if (panel_screen) //hide panel
                panel_screen.style.display = "none";
            //change title of window
            var wnd = document.getElementById("wsshSlideShowDialog");
            if (wnd) document.title = wnd.getAttribute("full_screen_title") + sourcePageTitle;
            // set min image size for full screen
            this.minSize = { width: 2, height: 2 };
        }
        else
        {
            document.title = document.title + sourcePageTitle;
        }

        if (this.logger.enabled) this.logger.debug("onLoad: turn on full screen mode");
        //go to full screen mode
        setTimeout(gSlideShow.beginFullscreen, 100);

        this.loadConfig();

        this.downloadNextImage();

        if (!fullScreenMode && wsshConfig.slideAutoStart)
            this.onPlay();
        else
            swapImages("pause", "play");
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onLoad: ", ex);
    }
}

gSlideShow.onUnLoad = function (event)
{
    try
    {
        gSlideShow.endFullscreen(window);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onUnLoad: ", ex);
    }
}

gSlideShow.addImages = function (images)
{
    try
    {
        images = this.removeDuplicates(images);

        if (images.length == 0)
            return window;

        var lastIndex = this.imgSources.length;

        for (var i = 0, count = images.length; i < count; ++i)
        {
            this.imgSources[lastIndex + i] = images[i];
        }

        var panel_screen = document.getElementById("slideshow_panel_screen");
        if (panel_screen) panel_screen.style.display = "";

        var wnd = document.getElementById("wsshSlideShowDialog");
        if (wnd) document.title = wnd.getAttribute("original_title");

        if (!this.loadingInProgress) // we need to start download
        {
            this.idxInProgress = lastIndex - 1;
            this.downloadNextImage();

            if (wsshConfig.slideAutoStart)
                this.onPlay();
        }

        return window;
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("addImages: ", ex);
    }
}

gSlideShow.removeDuplicates = function (newImages)
{
    if (!newImages || !newImages.length)
        return [];

    var result = new Array();

    var found;
    for (var i = 0, count = newImages.length; i < count; ++i)
    {
        found = false;
        for (var j = 0; j < this.imgSources.length && !found; ++j)
        {
            if (newImages[i] == this.imgSources[j])
                found = true;
        }
        for (var k = 0; k < result.length && !found; ++k)
        {
            if (newImages[i] == result[k])
                found = true;
        }

        if (!found)
            result[result.length] = newImages[i];
    }

    return result;
}

gSlideShow.beginFullscreen = function ()
{
    try
    {
        if (typeof window.fullScreen != 'undefined')
            window.fullScreen = true;

        if (!yrprxFactory.nativeHelperExists())
            return;

        gSlideShow.nativeHelper = yrprxFactory.createNativeHelper();
        gSlideShow.nativeHelper.beginFullScreen(window);
    }
    catch(ex)
    {
        if (gSlideShow.logger.enabled) gSlideShow.logger.error("beginFullscreen: ", ex);
    }
}

gSlideShow.endFullscreen = function ()
{
    try
    {
        if (!gSlideShow.nativeHelper)
            return;

        gSlideShow.nativeHelper.endFullScreen(window);
    }
    catch(ex)
    {
        if (gSlideShow.logger.enabled) gSlideShow.logger.error("endFullscreen: ", ex);
    }
}

gSlideShow.loadConfig = function ()
{
    this.interval = wsshConfig.slideShowDelay;
    this.updateIntervalTooltips();

    if (this.shrinkMode != wsshConfig.shrinkToFitScreen)
    {
        if (this.shrinkMode)
            swapImages("shrink_on", "shrink_off");
        else
            swapImages("shrink_off", "shrink_on");

        this.shrinkMode = wsshConfig.shrinkToFitScreen;
    }

    if (this.zoomMode != wsshConfig.zoomToFitScreen)
    {
        if (this.zoomMode)
            swapImages("zoom_on", "zoom_off");
        else
            swapImages("zoom_off", "zoom_on");

        this.zoomMode = wsshConfig.zoomToFitScreen;
    }

    this.scrollStep = wsshConfig.imageScrollStep;
    this.zoomPercent = wsshConfig.imageZoomStep;

    if (this.repeatMode != wsshConfig.repeat)
    {
        if (this.repeatMode)
            swapImages("repeat_on", "repeat_off");
        else
            swapImages("repeat_off", "repeat_on");

        this.repeatMode = wsshConfig.repeat;
    }
}

gSlideShow.downloadNextImage = function ()
{
    try
    {
        if (this.logger.enabled) this.logger.debug("downloadNextImage... ");

        var loading = document.getElementById("loading");

        if (++this.idxInProgress >= this.imgSources.length)
        {
            if (this.logger.enabled) this.logger.debug("downloadNextImage: download complete: "+this.loadedCount+"/"+this.imgSources.length);
            // change mode
            this.loadingInProgress = false;
            // hide loading indicator
            if (loading) { loading.style.display = "none"; loading.style.visibility = "hidden"; }
            // free image instance
            this.imgInProgress = null;

            if (!this.loadedCount)// nothing to show
            {
                yrprxPrompt.alert(document.title, wsshResources["webslideshow.can.not.load.images"]);
                closeWindow();
            }

            return;
        }

        if (this.logger.enabled) this.logger.debug("downloadNextImage: " + (this.idxInProgress+1) + "/" + this.imgSources.length);

        this.imgInProgress = new Image;

        this.imgInProgress.onload = function (e)
        {
            try
            {
                //prevent other events
                this.onload = null;

                if (gSlideShow.logger.enabled) gSlideShow.logger.debug("downloadNextImage: onload event occured...");

                if (this.width >= gSlideShow.minSize.width && this.height >= gSlideShow.minSize.height)
                    gSlideShow.onImageLoaded(this);
                else
                    gSlideShow.skipImage();
            }
            catch(ex)
            {
                if (gSlideShow.logger.enabled) gSlideShow.logger.error("downloadNextImage: onload: ", ex);
            }
        }

        this.imgInProgress.onerror = function (e)
        {
            try
            {
                //prevent other events
                this.onerror = null;

                if (gSlideShow.logger.enabled) gSlideShow.logger.debug("downloadNextImage: onerror event occured...");

                gSlideShow.skipImage();
            }
            catch(ex)
            {
                if (gSlideShow.logger.enabled) gSlideShow.logger.error("downloadNextImage: onerror: ", ex);
            }
        }

        var imgSource = this.imgSources[this.idxInProgress];

        if (this.logger.enabled) this.logger.debug("downloadNextImage: starting download: " + imgSource);
        //set mode
        this.loadingInProgress = true;
        // update loading indicator
        if (loading)
        {
            loading.style.display = ""; 
            loading.style.visibility = "visible";
            // change tooltip
            setTooltip(loading, (this.idxInProgress+1) + "/" + this.imgSources.length);
        }

        // set source / start download

        if (0 != imgSource.indexOf("javascript:"))
            this.imgInProgress.src = imgSource;
        else // javascript template
        {
            var urlResolver = null;
            eval(imgSource.replace("javascript:", "urlResolver="));
            imgSource = urlResolver;

            gSlideShow.logger.debug("downloadNextImage: call url resolver javascript...");
            imgSource(function(url) {
                if (!url)
                {
                    gSlideShow.logger.debug("downloadNextImage: url was not resolved...");
                    gSlideShow.skipImage();
                }
                else
                {
                    if (gSlideShow.logger.enabled) gSlideShow.logger.debug("downloadNextImage: url was resolved: " + url);
                    // save URL instead of javascript
                    gSlideShow.imgSources[gSlideShow.idxInProgress] = url;
                    // set source to start download
                    gSlideShow.imgInProgress.src = url;
                }
            });
        }
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("downloadNextImage: ", ex);
    }
}

gSlideShow.skipImage = function ()
{
    try
    {
        if (this.logger.enabled) this.logger.debug("skipImage: " + this.idxInProgress);

        // set to null to prevent saving
        this.imgSources[this.idxInProgress] = null;

        this.downloadNextImage();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("skipImage: ", ex);
    }
}

gSlideShow.onImageLoaded = function (img)
{
    try
    {
        if (this.logger.enabled) this.logger.debug("onImageLoaded: " + this.idxInProgress);

        var image = createImage(this.loadedCount);

        image.setAttribute("original_width", img.width);
        image.setAttribute("original_height", img.height);

        image.setAttribute("validate", "never");
        image.setAttribute("src", img.src);

        var slideshow_screen = document.getElementById("slideshow_screen");
        // add to screen
        slideshow_screen.insertBefore(image, document.getElementById("slideshow_panel_screen"));

        ++this.loadedCount;
        if (this.logger.enabled) this.logger.debug("onImageLoaded: " + this.loadedCount);

        if (this.idxCurrent == -1)
        {
            //try again to be sure
            if (typeof window.fullScreen != 'undefined')
                window.fullScreen = true;
            // hide control panel
            hideControlPanel();
            // hide big progress
            var slideshow_loading_screen = document.getElementById("slideshow_loading_screen");
            slideshow_loading_screen.style.display = "none";
            //show image
            this.showImage(0);
        }

        this.downloadNextImage();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onImageLoaded: ", ex);
    }
}

gSlideShow.isEnabledShowNexImage = function ()
{
    return this.idxCurrent+1 < this.loadedCount;
}

gSlideShow.showNextImage = function (force)
{
    try
    {
        if (this.timeElapsed < this.interval && !force)
            return;

        if (this.idxCurrent+1 >= this.loadedCount)
        {
            if (this.loadingInProgress || force) //prevent also repeat by keyboard
                return;

            if (!this.repeatMode)
                this.onPause();
            else
            {
                this.showImage(0);
                this.disablePrevImageButtons();
            }
        }
        else
        {
            if (this.logger.enabled) this.logger.debug("showNextImage: " + (this.idxCurrent + 1) + "/" + this.loadedCount);

            this.showImage(this.idxCurrent + 1);

            if (this.idxCurrent >= this.loadedCount - 1)
                this.disableNextImageButtons();
        }
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("showNextImage: ", ex);
    }
}

gSlideShow.isEnabledShowPrevImage = function ()
{
    return this.idxCurrent > 0;
}

gSlideShow.showPrevImage = function ()
{
    try
    {
        if (!this.isEnabledShowPrevImage())
            return;

        if (this.logger.enabled) this.logger.debug("showPrevImage: " + (this.idxCurrent+1) + "/" + this.loadedCount);

        this.showImage(this.idxCurrent-1);
        if (this.idxCurrent <= 0)
            this.disablePrevImageButtons();
        this.onPause();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("showPrevImage: ", ex);
    }
}

gSlideShow.isEnabledShowFirstImage = function ()
{
    return this.isEnabledShowPrevImage();
}

gSlideShow.showFirstImage = function ()
{
    try
    {
        if (!this.isEnabledShowFirstImage())
            return;

        if (this.logger.enabled) this.logger.debug("showFirstImage: 0/" + this.loadedCount);

        this.showImage(0);
        this.disablePrevImageButtons();
        this.onPause();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("showFirstImage: ", ex);
    }
}

gSlideShow.isEnabledShowLastImage = function ()
{
    return this.isEnabledShowNexImage();
}

gSlideShow.showLastImage = function ()
{
    try
    {
        if (!this.isEnabledShowLastImage())
            return;

        if (this.logger.enabled) this.logger.debug("showLastImage: " + this.loadedCount + "/" + this.loadedCount);

        this.showImage(this.loadedCount-1);
        this.disableNextImageButtons();
        this.onPause();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("showLastImage: ", ex);
    }
}

gSlideShow.showImage = function (idx)
{
    try
    {
        if (idx < 0 || idx >= this.loadedCount)
            return;

        if (this.logger.enabled) this.logger.debug("showImage: " + (idx+1) + "/" + this.loadedCount);

        if (this.idxCurrent != -1)
        {
            var visibleImage = document.getElementById("image"+this.idxCurrent);
            if (visibleImage) visibleImage.style.visibility = "hidden";
            this.imgCurrent = null;
        }

        var image = document.getElementById("image"+idx);
        if (image)
        {
            var imgInfo = this.imgCurrentInfo;
            var originalSize = imgInfo.originalSize;
            originalSize.width = 1*image.getAttribute("original_width");
            originalSize.height = 1*image.getAttribute("original_height");

            if (wsshSlideShowHelper.isScreenBounded(originalSize.width, originalSize.height)) // image less or equal to screen
            {
                if (this.zoomMode)
                    imgInfo.size = wsshSlideShowHelper.getScreenZoomedSize(originalSize.width, originalSize.height);
                else
                    imgInfo.size = {width:originalSize.width,height:originalSize.height};
            }
            else
            {
                if (this.shrinkMode)
                    imgInfo.size = wsshSlideShowHelper.getScreenBoundedSize(originalSize.width, originalSize.height);
                else
                    imgInfo.size = {width:originalSize.width,height:originalSize.height};
            }

            imgInfo.position.left = Math.floor((screen.width-imgInfo.size.width)/2);
            imgInfo.position.top = Math.floor((screen.height-imgInfo.size.height)/2);

            setPosition(image, imgInfo.position.left,  imgInfo.position.top);
            setImageSize(image, imgInfo.size);

            image.style.visibility = "visible";
            this.idxCurrent = idx;
            this.imgCurrent = image;
        }

        this.timeElapsed = 0;
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("showImage: ", ex);
    }
}

gSlideShow.saveImage = function ()
{
    try
    {
        if (this.logger.enabled) this.logger.debug("saveImage: " + this.idxCurrent);

        if (this.idxCurrent == -1)
            throw "Nothing to save";

        var image = document.getElementById("image"+this.idxCurrent);
        if (!image || !image.src)
            throw "Internal error! Cannot get current image!";

        this.onPause();

        wsshSaveImageHelper.saveImage(image.src);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("saveImage: ", ex);
    }
}

gSlideShow.saveAllImages = function ()
{
    try
    {
        if (this.imgSources.length < 2)
            return;

        this.onPause();

        wsshSaveImageHelper.saveAllImages(this.imgSources);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("saveImage: ", ex);
    }
}

gSlideShow.onKeyPress = function (event)
{
    try
    {
        event = yrprxBrowser.getEvent(event);

        if (this.logger.enabled) this.logger.debug("onKeyPress: keyCode= " + event.keyCode + ", charCode=" + event.charCode);

        var modifiers = yrprxKeyboardHelper.getKeyboardModifiers(event);

        var keyCode = event.keyCode;
        if (!keyCode)
        {
            // remap keys
            switch (event.charCode)
            {
            case yrprxKeyboardHelper.VK_SPACE:
                keyCode = yrprxKeyboardHelper.VK_SPACE;
                break;
            case 43: // "+"
                keyCode = 107;
                break;
            case 45: // "-"
                keyCode = 109;
                break;
            case 47: // "/"
                keyCode = 111;
                break;
            case 42: // "*"
                keyCode = 106;
                break;
            case 79: // O
            case 111: // O
                keyCode = 79;
                break;
            case 82: // R
            case 114: // R
                keyCode = 82;
                break;
            case 83: // S
            case 115: // S
                keyCode = 83;
                break;
            }
        }

        switch (keyCode)
        {
        case yrprxKeyboardHelper.VK_LEFT:
            if (event.ctrlKey || modifiers == yrprxKeyboardHelper.KEY_MASK_ACCEL)
                this.showPrevImage();
            else
                this.scrollLeft();
            break;
        case yrprxKeyboardHelper.VK_RIGHT:
            if (event.ctrlKey || modifiers == yrprxKeyboardHelper.KEY_MASK_ACCEL)
                this.showNextImage(true);
            else
                this.scrollRight();
            break;
        case yrprxKeyboardHelper.VK_UP:
            this.scrollUp();
            break;
        case yrprxKeyboardHelper.VK_DOWN:
            this.scrollDown();
            break;
        case yrprxKeyboardHelper.VK_ESCAPE:
            closeWindow();
            break;
        case yrprxKeyboardHelper.VK_SPACE:
            if (this.playMode)
                this.onPause();
            else
            {
                this.onPlay();
                //hide panel
                if (this.loadedCount > 0)
                    hideControlPanel();
            }
            break;
        case yrprxKeyboardHelper.VK_PAGE_UP:
            this.showPrevImage();
            break;
        case yrprxKeyboardHelper.VK_PAGE_DOWN:
            this.showNextImage(true);
            break;
        case yrprxKeyboardHelper.VK_HOME:
            this.showFirstImage();
            break;
        case yrprxKeyboardHelper.VK_END:
            this.showLastImage();
            break;
        case 107: // "+"
            this.zoomIn();
            break;
        case 109: // "-"
            this.zoomOut();
            break;
        case 111: // "/"
            if (!this.shrinkMode && !this.zoomMode)
                this.zoomToOriginalSize();
            else
            {
                if (this.shrinkMode)
                    this.toggleShrinkMode();
                if (this.zoomMode)
                    this.toggleZoomMode();
            }
            break;
        case 106: // "*"
            if (this.shrinkMode && this.zoomMode)
                this.updateImage();
            else
            {
                if (!this.shrinkMode)
                    this.toggleShrinkMode();
                if (!this.zoomMode)
                    this.toggleZoomMode();
            }
            break;
        case 79: // O
            if (event.ctrlKey || modifiers == yrprxKeyboardHelper.KEY_MASK_ACCEL)
            {
                this.onPause();
                wsshWindowManager.openOptions();
                this.loadConfig();
            }
            break;
        case 82: // R
            if (event.ctrlKey || modifiers == yrprxKeyboardHelper.KEY_MASK_ACCEL)
                this.toggleRepeatMode();
            break;
        case 83: // S
            if (event.ctrlKey || modifiers == yrprxKeyboardHelper.KEY_MASK_ACCEL)
            {
                if (!event.shiftKey)
                    this.saveImage();
                else
                    this.saveAllImages();
            }
            break;
        }
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onKeyPress: ", ex);
    }
}

gSlideShow.toggleShrinkMode = function ()
{
    try
    {
        if (this.shrinkMode)
            swapImages("shrink_on", "shrink_off");
        else
            swapImages("shrink_off", "shrink_on");

        this.shrinkMode = !this.shrinkMode;

        this.updateImage();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("toggleShrinkMode: ", ex);
    }
}

gSlideShow.toggleZoomMode = function ()
{
    try
    {
        if (this.zoomMode)
            swapImages("zoom_on", "zoom_off");
        else
            swapImages("zoom_off", "zoom_on");

        this.zoomMode = !this.zoomMode;

        this.updateImage();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("toggleZoomMode: ", ex);
    }
}

gSlideShow.toggleRepeatMode = function ()
{
    try
    {
        if (this.repeatMode)
            swapImages("repeat_on", "repeat_off");
        else
            swapImages("repeat_off", "repeat_on");

        this.repeatMode = !this.repeatMode;
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("toggleRepeatMode: ", ex);
    }
}

gSlideShow.updateImage = function ()
{
    try
    {
        this.showImage(this.idxCurrent);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("updateImage: ", ex);
    }
}

gSlideShow.onPause = function ()
{
    try
    {
        if (this.logger.enabled) this.logger.debug("onPause...");

        if (!this.playMode || !this.timer)
            return;

        clearInterval(this.timer);
        this.timer = null;

        this.playMode = false;
        swapImages("pause", "play");

        if (!wsshConfig.showPanelOnlyWhenMouseOverIt)
            showControlPanel();

        if (this.logger.enabled) this.logger.debug("onPause: stopped successfully...");
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onPause: ", ex);
    }
}

gSlideShow.isEnabledPlay = function ()
{
    return this.loadingInProgress || this.repeatMode || this.isEnabledShowNexImage();
}

gSlideShow.onPlay = function ()
{
    try
    {
        if (!this.isEnabledPlay())
            return;

        if (this.logger.enabled) this.logger.debug("onPlay...");

        if (this.playMode)
            return;

        //reset time
        this.timeElapsed = 0;
        // update timer
        this.timer = setInterval(function()
        {
            gSlideShow.onTimer();
        }, 1000);

        this.playMode = true;

        swapImages("play", "pause");

        if (this.logger.enabled) this.logger.debug("onPlay: started successfully...");
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onPlay: ", ex);
    }
}

gSlideShow.onTimer = function ()
{
    try
    {
        ++this.timeElapsed;
        this.showNextImage();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onTimer: ", ex);
    }
}

gSlideShow.onSlower = function ()
{
    try
    {
        ++this.interval;
        // save settings
        wsshConfig.slideShowDelay = this.interval;
        // update tooltips
        this.updateIntervalTooltips();
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onSlower: ", ex);
    }
}

gSlideShow.isEnabledFaster = function ()
{
    return this.interval > MIN_SLIDESHOW_DELAY;
}

gSlideShow.onFaster = function ()
{
    try
    {
        if (this.interval == MIN_SLIDESHOW_DELAY)
            return;

        if (this.interval > MIN_SLIDESHOW_DELAY)
            --this.interval;
        else
            this.interval = MIN_SLIDESHOW_DELAY;
        // save settings
        wsshConfig.slideShowDelay = this.interval;
        // update tooltips
        this.updateIntervalTooltips();

        if (this.interval == MIN_SLIDESHOW_DELAY)
            setDefaultImageFor("plus");
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("onFaster: ", ex);
    }
}

gSlideShow.updateIntervalTooltips = function ()
{
    try
    {
        this.updateIntervalTooltip("minus");
        this.updateIntervalTooltip("plus");
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("updateIntervalTooltips: ", ex);
    }
}

gSlideShow.updateIntervalTooltip = function (elemId)
{
    try
    {
        var elem = document.getElementById(elemId);
        if (!elem)
            return;

        var tooltipFormat = elem.getAttribute("tooltip_format");
        if (!tooltipFormat) return;

        var tooltip = tooltipFormat.replace("@interval@", this.interval);

        setTooltip(elem, tooltip);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("updateIntervalTooltip: ", ex);
    }
}

gSlideShow.onMouseOverPanel = function (event)
{
    try
    {
        var target = yrprxBrowser.getEventTarget(event);

        if (target.id == "slideshow_panel_screen")
            showControlPanel();
    }
    catch(ex)
    {
        if (gSlideShow.logger.enabled) gSlideShow.logger.error("onMouseOverPanel: ", ex);
    }
}

gSlideShow.onMouseOutPanel = function (event)
{
    try
    {
        var target = yrprxBrowser.getEventTarget(event);
        var relatedTarget = yrprxBrowser.getEventRelatedTarget(event);

        if (relatedTarget && relatedTarget.id != "slideshow_panel" && relatedTarget.id != "slideshow_panel_screen"
                && !yrprxHTMLContentHelper.hasParentWithId(relatedTarget, "slideshow_panel_screen"))
            hideControlPanel();
    }
    catch(ex)
    {
        if (gSlideShow.logger.enabled) gSlideShow.logger.error("onMouseOutPanel: ", ex);
    }
}

gSlideShow.zoomIn = function ()
{
    try
    {
        if (!this.imgCurrent)
            return;

        var imgInfo = this.imgCurrentInfo;

        var originalSize = imgInfo.originalSize;
        var zoomWidth = Math.floor(originalSize.width*this.zoomPercent/100);
        var zoomHeight = Math.floor(originalSize.height*this.zoomPercent/100);

        var size = imgInfo.size;
        size.width += zoomWidth;
        size.height += zoomHeight;

        var position = imgInfo.position;
        position.left -= Math.floor(zoomWidth/2);
        position.top -= Math.floor(zoomHeight/2);

        setPosition(this.imgCurrent, position.left,  position.top);
        setImageSize(this.imgCurrent, size);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("scrollLeft: ", ex);
    }
}

gSlideShow.zoomOut = function ()
{
    try
    {
        if (!this.imgCurrent)
            return;

        var imgInfo = this.imgCurrentInfo;

        var size = imgInfo.size;
        var zoomWidth = Math.floor(size.width*this.zoomPercent/100);
        var zoomHeight = Math.floor(size.height*this.zoomPercent/100);

        if ((size.width -= zoomWidth) < 0)
            size.width = 0;
        if ((size.height -= zoomHeight) < 0)
            size.height = 0;

        var position = imgInfo.position;
        if (size.width < screen.width)
            position.left = Math.floor((screen.width-size.width)/2);
        else
        {
            position.left += Math.floor(zoomWidth/2);
            if (position.left > 0)
                position.left = 0;
            else if (position.left+size.width < screen.width)
                position.left = screen.width - size.width;
        }
        if (size.height < screen.height)
            position.top = Math.floor((screen.height-size.height)/2);
        else
        {
            position.top += Math.floor(zoomHeight/2);
            if (position.top > 0)
                position.top = 0;
            else if (position.top+size.height < screen.height)
                position.top = screen.height - size.height;
        }

        setPosition(this.imgCurrent, position.left,  position.top);
        setImageSize(this.imgCurrent, size);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("scrollLeft: ", ex);
    }
}

gSlideShow.zoomToOriginalSize = function ()
{
    try
    {
        if (!this.imgCurrent)
            return;

        var imgInfo = this.imgCurrentInfo;

        var size = imgInfo.size;

        var originalSize = imgInfo.originalSize;
        size.width = originalSize.width;
        size.height = originalSize.height;

        var position = imgInfo.position;
        position.left = Math.floor((screen.width-size.width)/2);
        position.top = Math.floor((screen.height-size.height)/2);

        setPosition(this.imgCurrent, position.left,  position.top);
        setImageSize(this.imgCurrent, size);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("zoomToOriginalSize: ", ex);
    }
}

gSlideShow.scrollLeft = function ()
{
    try
    {
        if (!this.imgCurrent)
            return;

        var position = this.imgCurrentInfo.position;
        if (position.left >= 0)
            return

        position.left += this.scrollStep;
        if (position.left > 0)
            position.left = 0;

        setPositionLeft(this.imgCurrent, position.left);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("scrollLeft: ", ex);
    }
}

gSlideShow.scrollRight = function ()
{
    try
    {
        if (!this.imgCurrent)
            return;

        var size = this.imgCurrentInfo.size;
        var position = this.imgCurrentInfo.position;
        if (position.left+size.width <= screen.width)
            return

        position.left -= this.scrollStep;
        if (position.left+size.width < screen.width)
            position.left = screen.width - size.width;

        setPositionLeft(this.imgCurrent, position.left);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("scrollDown: ", ex);
    }
}

gSlideShow.scrollUp = function ()
{
    try
    {
        if (!this.imgCurrent)
            return;

        var position = this.imgCurrentInfo.position;
        if (position.top >= 0)
            return

        position.top += this.scrollStep;
        if (position.top > 0)
            position.top = 0;

        setPositionTop(this.imgCurrent, position.top);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("scrollUp: ", ex);
    }
}

gSlideShow.scrollDown = function ()
{
    try
    {
        if (!this.imgCurrent)
            return;

        var size = this.imgCurrentInfo.size;
        var position = this.imgCurrentInfo.position;
        if (position.top+size.height <= screen.height)
            return

        position.top -= this.scrollStep;
        if (position.top+size.height < screen.height)
            position.top = screen.height - size.height;

        setPositionTop(this.imgCurrent, position.top);
    }
    catch(ex)
    {
        if (this.logger.enabled) this.logger.error("scrollDown: ", ex);
    }
}

gSlideShow.disablePrevImageButtons = function ()
{
    setDefaultImageFor("first");
    setDefaultImageFor("previous");
}

gSlideShow.disableNextImageButtons = function ()
{
    setDefaultImageFor("last");
    setDefaultImageFor("next");
    // disable play button too
    setDefaultImageFor("play");
}
